<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="小程序">
                <a-select placeholder="请选择" v-model="queryParam.id" @change="getList()">
                  <a-select-option :value="item.id" v-for="item of idList" :key="item.id">
                    {{ item.app_name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="素材名称">
                <a-input placeholder="请输入素材名称" v-model="queryParam.material_name" allowClear></a-input>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div></div>

        <a-button type="primary" @click="$router.push({ name: 'marketingMaterialEdit' })" icon="plus">新建</a-button>
      </div>

      <a-table
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="record => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
      >
        <span slot="mainTitle" slot-scope="text, record">
          {{ record.info_json.mainTitle }}
        </span>

        <span slot="subTitle" slot-scope="text, record">
          {{ record.info_json.subTitle }}
        </span>

        <span slot="sellingTagMulti" slot-scope="text, record">
          {{ getSellingTagMulti(record.info_json.sellingTagMulti) }}
        </span>

        <span slot="actionPoint" slot-scope="text, record">
          {{ record.info_json.actionPoint || '' }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { listMarketingMaterial, delMarketingMaterial } from '@/api/marketing/alipayPromotion'
import { alipayList } from '@/api/setting/applet'

const columns = [
  {
    title: 'ID',
    dataIndex: 'material_id',
    width: 150
  },
  {
    title: '素材名称',
    dataIndex: 'material_name'
  },

  {
    title: '主标题',
    scopedSlots: {
      customRender: 'mainTitle'
    }
  },
  {
    title: '副标题',
    scopedSlots: {
      customRender: 'subTitle'
    }
  },
  {
    title: '推荐标签',
    scopedSlots: {
      customRender: 'sellingTagMulti'
    }
  },
  {
    title: '行动点',
    scopedSlots: {
      customRender: 'actionPoint'
    }
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action'
    },
    width: 100
  }
]
export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      idList: [],
      queryParam: {
        material_name: '',
        id: ''
      },

      list: []
    }
  },

  mounted() {
    const hide = this.$message.loading('加载中', 0)

    alipayList()
      .then(res => {
        hide()
        if (!res.code && res.data && res.data.length) {
          this.idList = res.data
          this.queryParam.id = res.data[0].id
          this.getList()
        }
      })
      .catch(err => {
        hide()
      })
  },

  computed: {
    getSellingTagMulti() {
      return info => {
        if (Array.isArray(info)) return info.join('/')
        return info
      }
    },

    getStatus() {
      return status => {
        let text, color
        switch (status) {
          case 'EDITING':
            text = '编辑中'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case 'AUDITING':
            text = '审核中'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case 'PROMOTION':
            text = '可推广'
            color = '#65C817'
            break

          default:
            if (status === 'STOP_PROMOTION') {
              text = '停止推广'
            } else if (status === 'AUDIT_REJECT') {
              text = '审核驳回'
            } else if (status === 'INVALID') {
              text = '失效'
            } else if (status === 'PUNISHED') {
              text = '处罚'
            }
            color = '#FF4D4F'
            break
        }

        return [text, color]
      }
    }
  },

  methods: {
    // 获取数据列表
    async getList(page = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await listMarketingMaterial({
        ...queryParam,
        pageSize: String(pageSize),
        page: String(page)
      })

      if (res.code == 0) {
        let { count: total, data: list } = res
        this.total = total

        if (list && list.length) {
          list = list.map(el => {
            if (el.info_json) {
              el.info_json = JSON.parse(el.info_json)
            }

            return el
          })

          console.log('list,', list)
        }
        this.list = list || []
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handleItem(type, { material_id }) {
      switch (type) {
        case 'remove':
          delMarketingMaterial(material_id).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.getList()
              return
            }

            this.$message.error(res.msg)
          })
          break
      }
    }
  }
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}
</style>
